package com.wanxx.handler;


import com.wanxx.exception.AgeException;
import com.wanxx.exception.MyException;
import com.wanxx.exception.NameException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(value = MyException.class)
    public ModelAndView doMyException(MyException ex) {
        ModelAndView mv = new ModelAndView();

        if (ex.getClass() == NameException.class) {
            mv.addObject("tips", "正在处理name异常");
        }
        if (ex.getClass() == AgeException.class) {
            mv.addObject("tips", "正在处理age异常");
        }
        mv.addObject("msg", ex.getMessage());
        mv.addObject("ex", ex);
        mv.setViewName("myexception");
        return mv;
    }

    @ExceptionHandler
    public ModelAndView doOtherException(Exception ex) {
        ModelAndView mv = new ModelAndView();

        mv.addObject("tips", "正在处理其它类型异常");
        mv.addObject("msg", ex);
        mv.setViewName("otherexception");
        return mv;
    }
}
